import json

from flask import request,jsonify

from utils.rbac import rbac
from utils.jwtc import token1
from flask import Blueprint
from utils.acl import acl
from utils.redisconn import r
from utils.comm import get_data
from utils.db import db1
from utils.comm import get_courses


bp_user = Blueprint('user',__name__)

# 登陆
@bp_user.route('/login',methods=['GET'])
def login():
    name = request.args.get("name")
    password = request.args.get("password")
    # print(name,password)
    sql = "select * from user where name='%s' and password='%s'"%(name,password)
    res = db1.find(sql)
    print(res)
    if res:
          vurl = acl.get_page_reslist(res['id'])
          interfacelist = acl.get_interface_resource(res['id'])
          vurl1 = vurl+interfacelist
          key = "interface"+str(res['id'])
          r.insert_values(key,interfacelist)
          token = token1.encode_jwt(int(res["id"]), 60 * 60, "123")
          return jsonify({'code':200,'userid':res['id'],"token":token,"vurl":vurl1})
    else:
        return jsonify({"code":20020,"msg":'登陆失败'})


# 获取侧边栏 权限
@bp_user.route('/get_course')
def get_course():
    # uid = 1
    uid = int(request.args.get('uid'))
    sql = acl.get_recourse(uid)
    list = get_courses(sql)
    return jsonify({'code':200,'list':list})



# 穿梭框
@bp_user.route('/permission')
def ziyuan():

    sql = "select * from resource"

    res = db1.find_all(sql)

    return jsonify({'code':200,'list':res})

@bp_user.route('/quanxian',methods=["POST"])
def quanxian():

    data = get_data()
    sql = "delete from user_resource where usersid  = %d"  % int(data['uid'])
    db1.update(sql)
    for i in json.loads(str(data['resource_id'])):
        sql = "insert into user_resource(resourceid,usersid) values(%d,%d)" % (int(i),int(data['uid']))
        db1.update(sql)
    return jsonify({'code':200,'mes':'添加成功'})



# def getPormition(func):




@bp_user.route('/test1')
def test1():
    key = 'abac'
    r.hset_key(key,'ip','127.0.0.1')
    r.hset_key(key,'level','003')
    r.hset_key(key,'time',"['2021-10-10','2021-12-10']")
    values = r.hget_keys(key)

    for (k,v) in values.items():
        if str(k,encoding='utf8') == 'time':
            print('ssad')
            print(eval(v))
    return 'ok'

























